<%@ page import="java.util.List" %>
<%@ page import="bean.Banji" %>
<%@ page import="service.BanjiService" %>
<%@ page import="service.BanjiServiceImpl" %><%--
  Created by IntelliJ IDEA.
  User: zhangyading
  Date: 2021/8/9
  Time: 19:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>班级管理模块</title>
    <link href="./css/bootstrap.min.css" rel="stylesheet">
    <script src="./js/jquery-3.3.1.min.js" type="text/javascript"></script>
    <script src="./js/bootstrap.min.js" type="text/javascript"></script>
</head>
<div class="table-responsive" style="padding: 50px">
    <table class="table">
        <caption>
            <button class="btn btn-info" id="banji_add" data-toggle="modal" data-target="#BjModal">新增班级</button>
            <button class="btn btn-info" id="banji_delete">删除班级</button>
            <button class="btn btn-info" id="banji_modify" data-toggle="modal" data-target="#BjModal">修改班级</button>
            <button class="btn btn-info" id="banji_find">查询班级</button>
            <input type="text" id="b_code" placeholder="按班级查询" style="width: 130px;height: 30px">
        </caption>
        <thead>
            <th>选择</th>
            <th>班级名称</th>
            <th>年级</th>
            <th>班主任名称</th>
            <th>班级口号</th>
            <th>班级人数</th>
        </thead>
        <tbody id="tbody"></tbody>
        <%

            BanjiService banjiService = new BanjiServiceImpl ();
            List<Banji> banjiList = banjiService.BanjiShowService ();
            for (int i = 0;i <banjiList.size ();i++) {
        %>
        <tr>
            <td><input type="checkbox" bName="<%=banjiList.get(i).getbName()%>" value="<%=banjiList.get(i).getId()%>"></td>
            <td><%=banjiList.get(i).getbName()%></td>
            <td><%=banjiList.get(i).getbAge()%></td>
            <td><%=banjiList.get(i).gettName()%></td>
            <td><%=banjiList.get(i).getKouHao()%></td>
            <td><%=banjiList.get(i).getNum()%></td>
        </tr>
        <%
            }
        %>
    </table>
    <div class="modal" id="BjModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="banjiLabelAdd">
                        班级信息
                    </h4>
                </div>
                <form action="banjiAddServlet" method="post">
                    <input type="hidden" id="flag" name="flag">
                    <div class="modal-body">
                        <label for="b_bname">班级名称</label>
                        <input type="text" class="form-control" id="b_bname" name="bName"/>
                        <label for="b_bage">年级</label>
                        <input type="text" class="form-control" id="b_bage" name="bAge"/>
                        <label for="b_tname">班主任名称</label>
                        <input type="text" class="form-control" id="b_tname" name="tName"/>
                        <label for="b_kouhao">班级口号</label>
                        <input type="text" class="form-control" id="b_kouhao" name="kouHao"/>
                        <label for="b_num">班级人数</label>
                        <input type="text" class="form-control" id="b_num" name="bNum"/>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-info" id="save">提交</button>
                        <button type="reset" class="btn btn-info" data-dismiss="modal">取消</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<body>

</body>
</html>

<script type="text/javascript">

    //增加班级
    $("#banji_add").on("click",function () {
        $("#flag").val("add");
        alert ($("#flag").val());
    });

    //删除班级
    $("#banji_delete").on("click",function () {
        var checkboxs = $("input[type='checkbox']:checked");
        //处理没有勾选的情况
        if (checkboxs.length == 0){
            alert("请选择一个班级");
            return false;
        } else if (checkboxs.length > 1 ) {
            alert("只能选择一个班级");
            return false;
        } else {
            var bname = "bName=" + checkboxs.attr("bName");
            //获取模块路径
            var moudulePathName = window.document.location.pathname;
            //获取模块名称
            var moduleName = moudulePathName.substring(0,moudulePathName.substr(1).indexOf('/')+1);
            //拼接结果
            window.location.href = moduleName + "/banjiDeleteServlet?" + bname;
        }
    });
    //修改班级
    $("#banji_modify").on("click",function () {
        var checkboxs = $("input[type=checkbox]:checked");
        if (checkboxs.length == 0) {
            alert("请选择一个班级");
            return false;
        } else if (checkboxs.length > 1) {
            alert("只能选择一个班级");
            return false;
        } else {
            $("#flag").val(checkboxs[0].value)
        }
    });
    //查找班级
    $("#banji_find").on("click",function () {
        var codeLike = $(this).siblings("#b_code").val();
        alert(codeLike.length);
        if (codeLike.length == 0) {
            alert("必须输入班级名称");
            return false;
        } else {
            var moudulePathName = window.document.location.pathname;
            var mouduleName = moudulePathName.substring(0,moudulePathName.substr(1).indexOf('/')+1);
            window.location.href = mouduleName + "/banjiFindServlet?bName=" + codeLike;
        }
    })
</script>